package com.taotao.manage.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.taotao.common.constant.SystemConstant;
import com.taotao.common.utils.FastDFSClient;

/**  
 * @Title：PictureController.java   
 * @Description：
 * @Author：张朝阳
 * @Date：2019年7月6日 上午9:48:47   
 */
@Controller
public class PictureController {
	@Value("${FASTDFS_URL}")
	private String FASTDFS_URL;

	@RequestMapping("/pic/upload")
	@ResponseBody
	public String picUpload(MultipartFile uploadFile) {
		Map<String,Object> result = new HashMap<>();
		try {
			//接收上传的文件
			//取扩展名
			String originalFilename = uploadFile.getOriginalFilename();
			String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
			//上传到图片服务器
			FastDFSClient fastDFSClient = new FastDFSClient("classpath:client.conf");
			String url = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
			url = FASTDFS_URL + url;
			//响应上传图片的url
			result.put("error", SystemConstant.IN_VALID);
			result.put("url", url);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("error", SystemConstant.VALID);
			result.put("message", "图片上传失败");
		}
		return JSON.toJSONString(result);
	}
}
